﻿using IdentityServerManagement.ApiResources;
using IdentityServerManagement.ApiScopes;
using IdentityServerManagement.Clients;
using IdentityServerManagement.Devices;
using IdentityServerManagement.Grants;
using IdentityServerManagement.IdentityResources;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Xms.Abp.Modularity;
using Xms.Abp.Storage;
using Xms.Abp.Storage.DependencyInjection;

namespace IdentityServerManagement.Storage
{
    [DependsOn(
        typeof(IdentityServerManagementDomainModule),
        typeof(XmsAbpStorageModule)
        )]
    public class IdentityServerManagementStorageModule : XmsModule
    {
        public override void PreConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.PreConfigure<IIdentityServerBuilder>(
                builder =>
                {
                    builder.AddAbpStores();
                }
            );
        }

        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddStorage<IdentityServerStorage>(
                IdentityServerManagementDbProperties.DbTablePrefix,
                IdentityServerManagementDbProperties.DbSchema,
                options =>
                {
                    //options.AddDefaultRepositories<IIdentityServerStorage>();

                    options.AddRepository<Client, ClientRepository>();
                    options.AddRepository<ApiResource, ApiResourceRepository>();
                    options.AddRepository<ApiScope, ApiScopeRepository>();
                    options.AddRepository<IdentityResource, IdentityResourceRepository>();
                    options.AddRepository<PersistedGrant, PersistentGrantRepository>();
                    options.AddRepository<DeviceFlowCodes, DeviceFlowCodesRepository>();
                });
        }
    }
}
